package demo02;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

//客户端
public class TcpClient {

    public static void main(String[] args) {

        OutputStream outputStream = null;
        Socket socket = null;
        //要知道服务器地址
        //连接
        try {
            InetAddress serviceIP = InetAddress.getByName("127.0.0.1");
            //端口号
            int port = 9999;
            //创建一个socket连接
            socket = new Socket(serviceIP,port);
            //发送消息，IO流
            outputStream = socket.getOutputStream();
            outputStream.write("你好啊！message!".getBytes(StandardCharsets.UTF_8));

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (outputStream !=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


        }
    }
}
